﻿#include<iostream>
#include<boost/asio.hpp>
const int MAX_LENGTH = 1024;
int main() {
	try {
		boost::asio::ip::tcp::endpoint ep(boost::asio::ip::make_address("127.0.0.1"), 3333);
		boost::asio::io_context ioc;
		boost::asio::ip::tcp::socket socket(ioc);
		char request[MAX_LENGTH];
		boost::system::error_code err_code;
		socket.connect(ep, err_code);
		if (err_code) {
			std::cout << err_code.value() << " connect服务端失败:" << err_code.message() << std::endl;
			return 0;
		}
		while (1) {
			std::cout << "输入发送的数据：";
			std::cin.getline(request, MAX_LENGTH);
			int send_length = socket.send(boost::asio::buffer(request,strlen(request)));
			if (send_length <= 0) {
				return 0;
			}
			socket.receive(boost::asio::buffer(request,send_length));
			std::cout << "reply: ";
			std::cout.write(request, send_length);
			std::cout << "\n";
		}
	}
	catch (boost::system::system_error& e) {
		std::cerr << e.code() << e.what() << std::endl;
	}
	return 0;
}